
// Format amount accourding to Local Currency Setting

#include "ClocalCurrency.h"
#include <string.h>

ClocalCurrency::ClocalCurrency(localCurrency * aLocale)
{
	flags		= DOLLARSIGN;
	setLocale(aLocale);
}

void ClocalCurrency::setLocale(localCurrency * aLocale)
{
	locale		= *aLocale;
	curLength	= (int8)locale.currency_symbol.getLength();    
}

bool8 ClocalCurrency::changeFlag(int8 flag, bool8 turnOn)
{
	bool8 isOn = (bool8)(flags & flag);

	if (isOn != turnOn)
	{
		if (turnOn)
			flags |= flag;
		else
			flags &= ~flag;

		return true;
	}
	return false;
}

int8 ClocalCurrency::subCurSymbol(int8 offset, bool8 isNeg, bool8 beginning )
{	
	if( !(locale.scs_precedes && beginning) && !(!locale.scs_precedes && !beginning) )
    {
        return offset;
    }

	if( beginning && locale.scs_sep_by_space && !isNeg) 
    {
        buf[offset++] = ' ';
    }

    int subCurLength= (int8)locale.subcurrency_symbol.getLength();

	for(int i=subCurLength; i; i--,offset++ )
    {
		buf[offset] = *(locale.subcurrency_symbol+i-1);
    }

	if( !beginning && locale.scs_sep_by_space )
    {
        buf[offset++] = ' ';
    }

	return offset;
}



int8 ClocalCurrency::curSymbol(int8 offset, bool8 isNeg, bool8 beginning )
{
	if( !(flags & DOLLARSIGN) ) return offset;
	if( !(locale.cs_precedes && beginning) && !(!locale.cs_precedes && !beginning) ) return offset;

	if( beginning && locale.sep_by_space && !isNeg) buf[offset++] = ' ';

	for( int i=curLength; i; i--,offset++ )
		buf[offset] = *(locale.currency_symbol+i-1);

	if( !beginning && locale.sep_by_space ) buf[offset++] = ' ';
	return offset;
}


///////////////////////////////////////////////////////////////////////////////////////////////
/// @fn  ClocalCurrency::formatSubCurrency( int32 cents )
///
/// Takes the amount and returns a string formatted according to the locale subcurrency 
/// settings...e.g., using the cent sign instead of the dollar sign.
/// 
/// @param  cents   -   Amount to format, in subcurrency units
///
/// @return A string displaying the amount formatted according to subcurrency locale settings.
///
/// kharmon.  11-18-05.  #7145-7149.
///////////////////////////////////////////////////////////////////////////////////////////////
String
ClocalCurrency::formatSubCurrency( int32 cents )
{
    
	int8 len, i, j, isNeg, numDigits;

	isNeg = cents < 0;
	cents = ABS(cents);
	len = 0;

	len = subCurSymbol(len,isNeg,false);

	numDigits = 0;
	while (cents)
	{
		buf[len++] = '0' + (char)(cents % 10);
		cents /= 10;
		++numDigits;

		if ( numDigits % locale.grouping == 0 && cents)
        {
			buf[len++] = locale.thousands_sep[0];
        }
	}

	if (numDigits < 1)
    {
		buf[len++] = '0';
    }

	if (isNeg)
    {
        buf[len++] = '-';
    }

	len = subCurSymbol(len,isNeg,true);
	buf[len] = '\0';

	// reverse it
	for (i = 0, j = len - 1; i < j; i++, j--)
	{
		uint16 temp;
		temp=buf[i];
		buf[i]=buf[j];
		buf[j]=temp;
	}
	return buf;
}



String
ClocalCurrency::formatCurrency( int32 cents )
{
	int8 len, i, j, isNeg, numDigits, decSep;

	isNeg = cents < 0;
	cents = ABS(cents);
	len = 0;

	len = curSymbol(len,isNeg,false);

	if (flags & AUTOMATIC_CENTS)
	{
		int32 c=cents;
		// Automatic display of cents:
		// The flags are set so that decimal digits are shown if there are some cents
		// else whole dollars are shown
		flags &= ~SHOWCENTS;
		for( i=0; i<locale.frac_digits; i++,c/=10 )
			if( c%10 ) flags |= SHOWCENTS;
	}

	if (flags & SHOWCENTS)
	{
		numDigits = decSep = 0;
		while (cents)
		{
			buf[len++] = '0' + (char)(cents % 10);
			cents /= 10;
			++numDigits;

			if (numDigits == locale.frac_digits)
			{
				buf[len++] = locale.decimal_sep[0];
				decSep++;
			}

			if (numDigits>locale.frac_digits &&
					( (numDigits-locale.frac_digits) % locale.grouping)==0 && cents)
				buf[len++] = locale.thousands_sep[0];
		}

		if( locale.frac_digits )
			while (numDigits+decSep < locale.frac_digits+2 )
			{
				buf[len++] = (char)(numDigits+decSep != locale.frac_digits ? '0' : locale.decimal_sep[0]);
				++numDigits;
			}
	}
	else
	{
		if ( !(flags & AMOUNT_IN_DOLLARS) )
			for( i=0; i<locale.frac_digits; i++ ) cents /= 10;

		numDigits = 0;
		while (cents)
		{
			buf[len++] = '0' + (char)(cents % 10);
			cents /= 10;
			++numDigits;

			if ( numDigits % locale.grouping == 0 && cents)
				buf[len++] = locale.thousands_sep[0];
		}

		if (numDigits < 1)
			buf[len++] = '0';
	}

	if (isNeg)	buf[len++] = '-';
	len = curSymbol(len,isNeg,true);
	buf[len] = '\0';

	// reverse it
	//
	for (i = 0, j = len - 1; i < j; i++, j--)
	{
		uint16 temp;
		temp=buf[i];
		buf[i]=buf[j];
		buf[j]=temp;
	}
	return buf;
}
